package org.example.pingkunsheng.controller;

import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.exception.ErrorException;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.pingkunsheng.model.TTuitionWaiverModel;
import org.example.pingkunsheng.service.TTuitionWaiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Date: 2024/1/31 21:44
 */
@RestController
@RequestMapping("/tuition-waiver")
public class TTuitionWaiverController extends BaseController<String, TTuitionWaiverService, TTuitionWaiverModel>{

    @Autowired
    private TTuitionWaiverService tTuitionWaiverService;

    @PostMapping("/exportList")
    public void exportList(@RequestBody(required = false) Map<String,Object> params, HttpServletResponse response){
        tTuitionWaiverService.exportList(params,response);
    }

    @GetMapping("/getDetailByUserId")
    private ApiResult getDetailByUserId(String userId){
        TTuitionWaiverModel tTuitionWaiverModel = tTuitionWaiverService.searchOne(Where.build()
                .field("user_id").equal(userId)
                .and()
                .field("del_flag").equal(0));
        return ApiResult.success(tTuitionWaiverModel);
    }

    /**
     * 修改状态
     * @param params
     * @return
     */
    @PostMapping("/updateStatus")
    public ApiResult updateStatus(@RequestBody Map<String,Object> params){
        Object id = params.get("id");
        if (ObjectUtils.isEmpty(id)){
            throw new ErrorException("id不能为空");
        }
        Object status = params.get("status");
        if (ObjectUtils.isEmpty(status)){
            throw new ErrorException("审批状态不能为空！");
        }
        tTuitionWaiverService.updateStatus(params);
        return ApiResult.success();
    }

    /**
     * 提交
     * @param tTuitionWaiverModel
     * @return
     */
    @PostMapping("/submit")
    public ApiResult submit(@RequestBody TTuitionWaiverModel tTuitionWaiverModel){
        tTuitionWaiverModel.setStatus("1");
        if (ObjectUtils.isEmpty(tTuitionWaiverModel.getId())){
            tTuitionWaiverService.add(tTuitionWaiverModel);
        }else {
            tTuitionWaiverService.update(tTuitionWaiverModel);
        }
        return ApiResult.success();
    }


    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit,@RequestParam(required = false) Map<String, Object> params) {
        SearchPageResult<TTuitionWaiverModel> result = new SearchPageResult<>();
        Object name = params.get("name");
        Where where  = Where.build().field("del_flag").equal(0);
        if (!ObjectUtils.isEmpty(name)){
            where.and().field("student_name").equal(name);
        }
        int totalCount = tTuitionWaiverService.count(where);
        List<TTuitionWaiverModel> resultList = tTuitionWaiverService.search(where.limit(page, limit));
        result.setPage(page);
        result.setLimit(limit);
        result.setTotalCount(totalCount);
        result.setList(resultList);
        return ApiResult.success(result);
    }
}
